package com.tianwen.management.controller;

import com.tianwen.management.entity.Department;
import com.tianwen.management.result.Result;
import com.tianwen.management.service.DepartmentService;
import com.tianwen.management.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @program: personnel-management
 * @description
 * @author: tianwen
 * @create: 2023-05-17 09:04
 **/
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @GetMapping("/page/{page}/{pageSize}")
      public Result page(@PathVariable Integer page,@PathVariable Integer pageSize,String name){
       List<Department> departmentList=departmentService.page(page, pageSize, name);
          return Result.ok().data("departmentList",departmentList);
    }
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('部门增删改')")
    public Result add(@RequestBody Department department){
        departmentService.save(department);
        return Result.ok();
    }
    @GetMapping("/delete")
    @PreAuthorize("hasAuthority('部门增删改')")
    public Result delete(Long id){
        Boolean success = departmentService.removeAndEmpName(id);
        if(success){
            return Result.ok();
        }else{
            return Result.error();
        }

    }
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('部门增删改')")
    public Result update(@RequestBody Department department){
        Boolean success=departmentService.updateAndEmpName(department);
        if(success){
            return Result.ok();
        }else{
            return Result.error();
        }

    }
    @GetMapping("/findAll")
    public Result findAll(){
        List<Department> departmentList = departmentService.list();
        return Result.ok().data("departmentList",departmentList);
    }
}
